<template>
  <div>
    <el-table :data="records" border stripe>
      <el-table-column label="序号" type="index" width="80px" align="center">
      </el-table-column>
      <el-table-column label="名称" prop="skuName" width="width">
      </el-table-column>
      <el-table-column
        label="描述"
        prop="skuDesc"
        width="width"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="默认图片" prop="" width="110px">
        <template slot-scope="{ row, $index }">
          <div>
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 80px; height: 80px"
            />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="重量" prop="weight" width="80px">
      </el-table-column>
      <el-table-column label="价格" prop="price" width="80px">
      </el-table-column>
      <el-table-column label="操作" prop="" width="width">
        <template slot-scope="{ row, $index }">
          <el-button
            type="success"
            size="mini"
            icon="el-icon-sort-down"
            @click="sale(row)"
            v-if="row.isSale == 0"
          ></el-button>
          <el-button
            type="success"
            size="mini"
            icon="el-icon-sort-up"
            @click="cancel(row)"
            v-else
          ></el-button>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="edit"
          ></el-button>
          <el-button
            type="info"
            size="mini"
            icon="el-icon-info"
            @click="getSkuInfo(row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center"
      @size-change="sizeChange"
      @current-change="getSkuList"
      :current-page.sync="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->total, sizes,"
      :total="total"
      background
      :pager-count="7"
    >
    </el-pagination>

    <el-radio-group v-model="direction">
      <el-radio label="rtl">从右往左开</el-radio>
    </el-radio-group>
    <!-- 
      1.visible.sync:控制显示和隐藏
      2.direction:控制抽屉显示的方向
      3.before-close:关闭前的回调
      4.show-close:是否显示x按钮
      5.size:控制大小
     -->
    <el-drawer
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      :show-close="false"
      size="50%"
    >
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template>
            <el-tag
              style="margin-right: 10px"
              type="danger"
              v-for="(attr, index) in skuInfo.skuAttrValueList"
              :key="attr.id"
              >{{ attr.id }}-{{ attr.valueId }}</el-tag
            >
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" alt="" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  props: {},
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      total: 0,
      records: [],
      skuInfo: {},
      drawer: false,
      direction: "rtl",
    };
  },
  mounted() {
    this.getSkuList();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    async getSkuInfo(sku) {
      let result = await this.$API.Sku.reqSkuById(sku.id);
      this.drawer = true;
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
    edit() {
      this.$message.success("正在开发中");
    },
    async sale(row) {
      let result = await this.$API.Sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message.success("上架成功");
      }
    },
    cancel(row) {
      let result = await this.$API.Sku.reqCancel(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message.success("下架成功");
      }
    },
    async getSkuList(pager = 1) {
      this.page = pager;
      const { page, limit } = this;
      let result = await this.$API.Sku.reqGetSkuList(page, limit);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    sizeChange(size) {
      this.limit = size;
      this.getSkuList();
    },
  },
  components: {},
};
</script>

<style scoped lang="scss">
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
}
.el-col {
  margin: 10px 10px;
}
</style>
<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel_button {
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}
</style>